package com.runen.wnhz.runen.ui.adapter.puadapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.runen.wnhz.runen.R;
import com.runen.wnhz.runen.bean.LessonPriceChangeBean;
import com.runen.wnhz.runen.bean.OrderSureBean;

import org.simple.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/6/2 0002.
 */

public class HelpStudyAdapter extends RecyclerView.Adapter<HelpStudyAdapter.ViewHolder>{

    private Context context;

    private List<OrderSureBean.DataBean.OtherListBean>  otherListBeanList;

    private boolean isBindingSales;

    private double teachAssitancePrice = 0D;

    public HelpStudyAdapter(Context context, List<OrderSureBean.DataBean.OtherListBean> otherListBeanList, boolean isBindingSales) {
        this.context = context;
        this.otherListBeanList = otherListBeanList;
        this.isBindingSales = isBindingSales;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder viewHolder = null;
        View view= View.inflate(parent.getContext(), R.layout.adapter_item_helpstudy,null);
        viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final OrderSureBean.DataBean.OtherListBean otherListBean = otherListBeanList.get(position);
        holder.good_price.setText("￥".concat(otherListBean.getPrice()));
        Glide.with(context).load(otherListBean.getPic()).into(holder.goods_img);
        holder.goods_name.setText(otherListBean.getTitle());
        holder.checkbox_teachAssistance.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                LessonPriceChangeBean lessonPriceChangeBean;
                if(b){
                    lessonPriceChangeBean = new LessonPriceChangeBean(true,Double.parseDouble(otherListBean.getPrice()));
                }else{
                    lessonPriceChangeBean = new LessonPriceChangeBean(false,Double.parseDouble(otherListBean.getPrice()));
                }
                EventBus.getDefault().post(lessonPriceChangeBean);
            }
        });
        if(isBindingSales){
            holder.checkbox_teachAssistance.setEnabled(false);
            holder.checkbox_teachAssistance.setChecked(true);
        }else{
            holder.checkbox_teachAssistance.setEnabled(true);
            holder.checkbox_teachAssistance.setChecked(false);
        }
    }

    @Override
    public int getItemCount() {
        if(otherListBeanList != null){
            return otherListBeanList.size();
        }else {
            return 0;
        }
    }


    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.checkbox_teachAssistance)
        CheckBox checkbox_teachAssistance;
        @BindView(R.id.goods_img)
        ImageView goods_img;
        @BindView(R.id.goods_name)
        TextView goods_name;
        @BindView(R.id.good_price)
        TextView good_price;


        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
